// pages/classcirle/classcirleindex.js
var util = require('../../utils/util.js');
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    photo: null,
    show: false,
    hidden: true, //隐藏表单控件
    page: 1, //当前请求数据是第几页
    pageSize: 10, //每页数据条数
    hasMoreData: true, //上拉时是否继续请求数据，即是否还有更多数据
    list: null,
    itemList: ["图文", "视频"],
    title: null,
    year: 0,
    month: 0,
    userid: 0,
    commentValue: '',
    getInput: '',
    selectIndex: 0,
    receiverID: 0,
    receiverType: 0,
    receiverKidID: 0,
    classid: null,
    classlist: null,
    endTime: null,
    startTime: null,
  },



  showActionSheet(e) {
    wx.showActionSheet({
      itemList: this.data.itemList,
      success(e) {
        console.log("success")
        console.log(e)
        if (!e.camcle) {
          if (e.tapIndex == 0) {
            wx.navigateTo({
              url: '../newclasscirletext/newclasscirletext'
            })
          } else {
            wx.navigateTo({
              url: '../newclasscirlevideo/newclasscirlevedio'
            })
          }
          console.log(e.tapIndex)
        } else {
          console.log("cancle")
        }
      },
      fail(e) {
        console.log("fail")
        console.log(e)
      },
      complete(e) {
        console.log("complete")
        console.log(e)
      }
    })
  },

  // 获取分页列表
  getInfo: function (message) {
    var that = this;
    wx.showNavigationBarLoading() //在当前页面显示导航条加载动画
    wx.showLoading({ //显示 loading 提示框
      title: message,
    })
    var data = {
      "userid": app.globalData.userId,
      "schoolid": app.globalData.schoolId,
      "classid": that.data.classid,
      "pagenum": that.data.page,
      "pagesize": that.data.pageSize,
      "appType": app.globalData.appType,
      "appKey": app.globalData.appKey,
      "timeSpan": app.globalData.timespan,
      "mobileSerialnumber": ""
    }
    wx.request({
      url: app.globalData.serverUrl,
      data: {
        "data": JSON.stringify(data),
        'act': "get_show_class"
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
      },
      success: function (res) {
        console.log(res);
        wx.hideNavigationBarLoading()
        wx.hideLoading()
        if (res.data.status == 0) {
          var contentlistTem = that.data.list;
          if (res.data.data.length > 0) {
            if (that.data.page == 1) {
              contentlistTem = []
            }
            var list = res.data.data;

            list.forEach(function (item, index) {
              console.log(item); //这里的item就是从数组里拿出来的每一个每一组
              if (list[index].zanStr != null) {

                try {
                  //list[index].zanStr = decodeURIComponent(item.zanStr.replace(/%/g, '%25'))
                  list[index].zanStr = decodeURIComponent(item.zanStr)
                } catch (error) {
                  // 此处是负责例外处理的语句
                  list[index].zanStr = item.zanStr
                } finally {
                  // 此处是出口语句
                }

              }
              if (list[index].Content != null) {

                try {
                  //list[index].Content = decodeURIComponent(item.Content.replace(/%/g, '%25'))
                  list[index].Content = decodeURIComponent(item.Content)
                } catch (error) {
                  // 此处是负责例外处理的语句
                  list[index].Content = item.Content
                } finally {
                  // 此处是出口语句
                }


              }
              list[index].commentList.forEach(function (item1, index1) {
                if (list[index].commentList[index1].Content != null) {

                  try {
                    //list[index].commentList[index1].Content = decodeURIComponent(item1.Content.replace(/%/g, '%25'))
                    list[index].commentList[index1].Content = decodeURIComponent(item1.Content)
                  } catch (error) {
                    // 此处是负责例外处理的语句
                    list[index].commentList[index1].Content = item1.Content
                  } finally {
                    // 此处是出口语句
                  }


                }
                if (list[index].commentList[index1].Name != null) {

                  try {
                    //list[index].commentList[index1].Name = decodeURIComponent(item1.Name.replace(/%/g, '%25'))
                    list[index].commentList[index1].Name = decodeURIComponent(item1.Name)
                  } catch (error) {
                    // 此处是负责例外处理的语句
                    list[index].commentList[index1].Name = item1.Name
                  } finally {
                    // 此处是出口语句
                  }

                }
              })

            })

            console.log(list);

            if (list.length < that.data.pageSize) {
              that.setData({
                list: contentlistTem.concat(list),
                hasMoreData: false
              })
            } else {
              that.setData({
                list: contentlistTem.concat(list),
                hasMoreData: true,
                page: that.data.page + 1
              })
            }
          }
        } else {
          wx.hideNavigationBarLoading()
          wx.hideLoading()
          that.setData({
            list: [],
            hasMoreData: false
          })
        }

      },
      fail: function (res) {
        wx.hideNavigationBarLoading()
        wx.hideLoading()
        wx.showToast({
          title: "获取数据失败",
          icon: 'none',
          duration: 1000,
          mask: true
        })
      },
      complete: function (res) {
        console.log('submit complete');
      }
    })
  },

  onclasschange({
    detail
  }) {
    this.setData({
      classid: detail,
      page: 1
    });
    this.getInfo('正在加载数据...')

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    // 页面初始化 options为页面跳转所带来的参数
    // var that = this
    // that.getInfo('正在加载数据...')

    var that = this;
    var list = wx.getStorageSync('classlist');
    var classlist = []
    var classobj = {
      text: "全部班级",
      value: 0
    }
    classlist.push(classobj)
    for (var i = 0; i < list.length; i++) {
      var classobj = {
        text: list[i].className,
        value: list[i].classID
      }
      classlist.push(classobj)
    }
    this.setData({
      classid: classlist[0].value,
      classlist: classlist,
      userid: app.globalData.userId,
      photo: app.globalData.photo
    })


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.data.page = 1
    this.getInfo('正在加载数据...')
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.data.page = 1
    this.getInfo('正在刷新数据')
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.hasMoreData) {
      this.getInfo('加载更多数据')
    } else {
      wx.showToast({
        title: '没有更多数据',
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {


  },

  btnDelete: function (e) {
    var index = e.currentTarget.dataset.index;
    var contentid = this.data.list[index].ContentID
    var contenttype = this.data.list[index].ContentType
    var that = this
    wx.showModal({
      title: '班级圈',
      content: '是否删除',
      success: function (sm) {
        if (sm.confirm) {
          that.deleteContent(contentid, contenttype)
        } else if (sm.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  btnQuXiaopingbi: function (e) {
    var index = e.currentTarget.dataset.index;
    var contentid = this.data.list[index].ContentID
    var contenttype = this.data.list[index].ContentType
    var that = this
    wx.showModal({
      title: '班级圈',
      content: '是否取消屏蔽本条信息',
      success: function (sm) {
        if (sm.confirm) {
          that.pinbiContent(contentid, contenttype, 0)
        } else if (sm.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  btnPingbi: function (e) {
    var index = e.currentTarget.dataset.index;
    var contentid = this.data.list[index].ContentID
    var contenttype = this.data.list[index].ContentType
    var that = this
    wx.showModal({
      title: '班级圈',
      content: '是否屏蔽本条动态',
      success: function (sm) {
        if (sm.confirm) {
          that.pinbiContent(contentid, contenttype, 1)
        } else if (sm.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },


  btnZan: function (e) {
    var index = e.currentTarget.dataset.index;
    var contentid = this.data.list[index].ContentID
    var contenttype = this.data.list[index].ContentType
    this.zanPost(contentid, contenttype, 1)
  },





  btnCancelZan: function (e) {
    var index = e.currentTarget.dataset.index;
    var contentid = this.data.list[index].ContentID
    var contenttype = this.data.list[index].ContentType
    this.zanPost(contentid, contenttype, 0)
  },


  previewImg: function (e) {
    var index = e.currentTarget.dataset.index;
    console.log(index)
    var a = index.split(",")
    var url = a[1]
    var that = this;
    var imgArr = []
    for (var i = 0; i < that.data.list[a[0]].Attachments.length; i++) {
      imgArr.push(that.data.list[a[0]].Attachments[i].attachmentUrl)
    }
    wx.previewImage({
      current: url, //当前图片地址
      urls: imgArr, //所有要预览的图片的地址集合 数组形式
      success: function (res) {},
      fail: function (res) {},
      complete: function (res) {},
    })
  },

  zanPost: function (contentid, contenttype, iszan) {
    var that = this;
    var data = {
      "userid": app.globalData.userId,
      "schoolid": app.globalData.schoolId,
      "contentid": contentid,
      "contenttype": contenttype,
      "isZan": iszan,
      "appType": app.globalData.appType,
      "appKey": app.globalData.appKey,
      "timeSpan": app.globalData.timespan,
      "mobileSerialnumber": ""
    }
    wx.request({
      url: app.globalData.serverUrl,
      data: {
        "data": JSON.stringify(data),
        'act': "send_zan"
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
      },
      success: function (res) {
        console.log(res);
        if (res.data.status == 0) {
          var massage = "点赞"
          if (iszan == 0) {
            massage = "取消点赞"
          }
          wx.showToast({
            title: massage + "成功",
            icon: 'none',
            duration: 1000,
            mask: true
          })
          that.data.page = 1
          that.getInfo('正在刷新数据')
        } else {
          wx.showToast({
            title: massage + "失败",
            icon: 'none',
            duration: 1000,
            mask: true
          })
        }
      },
      fail: function (res) {
        wx.showToast({
          title: massage + "失败",
          icon: 'none',
          duration: 1000,
          mask: true
        })
      },
      complete: function (res) {
        console.log('submit complete');
      }
    })
  },
  deleteContent: function (contentid, contenttype) {
    var that = this;
    var data = {
      "userid": app.globalData.userId,
      "schoolid": app.globalData.schoolId,
      "contentid": contentid,
      "contenttype": contenttype,
      "appType": app.globalData.appType,
      "appKey": app.globalData.appKey,
      "timeSpan": app.globalData.timespan,
      "mobileSerialnumber": ""
    }
    wx.request({
      url: app.globalData.serverUrl,
      data: {
        "data": JSON.stringify(data),
        'act': "del_show"
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
      },
      success: function (res) {
        console.log(res);
        if (res.data.status == 0) {
          wx.showToast({
            title: "删除成功",
            icon: 'none',
            duration: 1000,
            mask: true
          })
          that.data.page = 1
          that.getInfo('正在刷新数据')
        } else {
          wx.showToast({
            title: "删除失败",
            icon: 'none',
            duration: 1000,
            mask: true
          })
        }
      },
      fail: function (res) {
        wx.showToast({
          title: "删除失败",
          icon: 'none',
          duration: 1000,
          mask: true
        })
      },
      complete: function (res) {
        console.log('submit complete');
      }
    })
  },

  pinbiContent: function (contentid, contenttype, isShielding) {
    var that = this;
    var tipmess = ""
    if (isShielding == 1) {
      tipmess = "屏蔽"
    } else {
      tipmess = "取消屏蔽"
    }
    var data = {
      "userid": app.globalData.userId,
      "schoolid": app.globalData.schoolId,
      "contentid": contentid,
      "contenttype": contenttype,
      "isShielding": isShielding,
      "appType": app.globalData.appType,
      "appKey": app.globalData.appKey,
      "timeSpan": app.globalData.timespan,
      "mobileSerialnumber": ""
    }
    wx.request({
      url: app.globalData.serverUrl,
      data: {
        "data": JSON.stringify(data),
        'act': "shielding_dongtai"
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
      },
      success: function (res) {
        console.log(res);
        if (res.data.status == 0) {

          wx.showToast({
            title: tipmess + "成功",
            icon: 'none',
            duration: 1000,
            mask: true
          })
          that.data.page = 1
          that.getInfo('正在刷新数据')
        } else {
          wx.showToast({
            title: tipmess + "失败",
            icon: 'none',
            duration: 1000,
            mask: true
          })
        }
      },
      fail: function (res) {
        wx.showToast({
          title: tipmess + "失败",
          icon: 'none',
          duration: 1000,
          mask: true
        })
      },
      complete: function (res) {
        console.log('submit complete');
      }
    })
  },







  onClose(e) {
    this.setData({
      show: true
    });
    if (this.data.show) {
      var index = e.currentTarget.dataset.index;
      this.setData({
        selectIndex: index,
        receiverID: 0,
        receiverType: 0,
        receiverKidID: 0
      })
    }
  },


  //touch start
  handleTouchStart: function (e) {
    this.startTime = e.timeStamp;
    //console.log(" startTime = " + e.timeStamp);  
  },

  //touch end
  handleTouchEnd: function (e) {
    this.endTime = e.timeStamp;
    //console.log(" endTime = " + e.timeStamp);  
  },



  handleLongPress: function (e) {
    //console.log("endTime - startTime = " + (this.endTime - this.startTime));
    var that = this
    console.log("长按");
    var index = e.currentTarget.dataset.index;
    console.log(index)
    var a = index.split(",")
    var id = a[1]
    var sendID = this.data.list[a[0]].commentList[id].UserID
    var isShielding = this.data.list[a[0]].commentList[id].isShielding
    var commentid = this.data.list[a[0]].commentList[id].CID
    var contentid = this.data.list[a[0]].ContentID
    var contenttype = this.data.list[a[0]].ContentType
    var UserType = this.data.list[a[0]].commentList[id].UserType
    var text = ""
    if (isShielding == 0) {
      text = "是否屏蔽评论"
    } else {
      text = "是否取消屏蔽评论"
    }

    if (app.globalData.userId != sendID) {
      if (UserType  == 1) {
        wx.showModal({
          title: '班级圈',
          content: text,
          success: function (sm) {
            if (sm.confirm) {
              if (isShielding == 0) {
                that.pinbiComment(contentid, contenttype, commentid, 1)
              } else {
                that.pinbiComment(contentid, contenttype, commentid, 0)
              }

            } else if (sm.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      }

    } else {
      if (UserType != 1) {
        wx.showModal({
          title: '班级圈',
          content: '是否删除评论',
          success: function (sm) {
            if (sm.confirm) {
              that.deleteComment(contentid, contenttype, commentid)
            } else if (sm.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      }else{
        wx.showModal({
          title: '班级圈',
          content: text,
          success: function (sm) {
            if (sm.confirm) {
              if (isShielding == 0) {
                that.pinbiComment(contentid, contenttype, commentid, 1)
              } else {
                that.pinbiComment(contentid, contenttype, commentid, 0)
              }

            } else if (sm.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      }

    }

  },

  pinbiComment: function (contentid, contenttype, commentid, isShielding) {
    var that = this;
    var tipmess = ""
    if (isShielding == 1) {
      tipmess = "屏蔽"
    } else {
      tipmess = "取消屏蔽"
    }
    var data = {
      "userid": app.globalData.userId,
      "schoolid": app.globalData.schoolId,
      "contentid": contentid,
      "commentid": commentid,
      "contenttype": contenttype,
      "isShielding": isShielding,
      "appType": app.globalData.appType,
      "appKey": app.globalData.appKey,
      "timeSpan": app.globalData.timespan,
      "mobileSerialnumber": ""
    }
    wx.request({
      url: app.globalData.serverUrl,
      data: {
        "data": JSON.stringify(data),
        'act': "shielding_comment"
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
      },
      success: function (res) {
        console.log(res);
        if (res.data.status == 0) {
          wx.showToast({
            title: tipmess + "成功",
            icon: 'none',
            duration: 1000,
            mask: true
          })
          that.data.page = 1
          that.getInfo('正在刷新数据')
        } else {
          wx.showToast({
            title: tipmess + "失败",
            icon: 'none',
            duration: 1000,
            mask: true
          })
        }
      },
      fail: function (res) {
        wx.showToast({
          title: tipmess + "失败",
          icon: 'none',
          duration: 1000,
          mask: true
        })
      },
      complete: function (res) {
        console.log('submit complete');
      }
    })
  },

  deleteComment: function (contentid, contenttype, commentid) {
    var that = this;
    var data = {
      "userid": app.globalData.userId,
      "schoolid": app.globalData.schoolId,
      "contentid": contentid,
      "commentid": commentid,
      "contenttype": contenttype,
      "appType": app.globalData.appType,
      "appKey": app.globalData.appKey,
      "timeSpan": app.globalData.timespan,
      "mobileSerialnumber": ""
    }
    wx.request({
      url: app.globalData.serverUrl,
      data: {
        "data": JSON.stringify(data),
        'act': "del_comment"
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
      },
      success: function (res) {
        console.log(res);
        if (res.data.status == 0) {
          wx.showToast({
            title: "删除成功",
            icon: 'none',
            duration: 1000,
            mask: true
          })
          that.data.page = 1
          that.getInfo('正在刷新数据')
        } else {
          wx.showToast({
            title: "删除失败",
            icon: 'none',
            duration: 1000,
            mask: true
          })
        }
      },
      fail: function (res) {
        wx.showToast({
          title: "删除失败",
          icon: 'none',
          duration: 1000,
          mask: true
        })
      },
      complete: function (res) {
        console.log('submit complete');
      }
    })
  },



  // 点击下拉显示框
  selectTap(e) {
    if (!this.data.show) {
      var index = e.currentTarget.dataset.index;
      this.setData({
        selectIndex: index,
        receiverID: 0,
        receiverType: 0,
        receiverKidID: 0
      })
    }

    this.setData({
      show: !this.data.show
    });



  },

  sendComment: function (e) {
    if (this.data.commentValue == null || this.data.commentValue.length == 0) {
      wx.showToast({
        title: "请输入评论内容",
        icon: 'none',
        duration: 1000,
        mask: true
      })
    } else {

      var contentid = this.data.list[this.data.selectIndex].ContentID
      var contenttype = this.data.list[this.data.selectIndex].ContentType
      this.commentPost(contentid, contenttype)
      // 
    }
  },

  getInput: function (e) {
    this.setData({
      commentValue: e.detail.value
    })
  },
  commentPost: function (contentid, contenttype) {
    var that = this;
    var data = {
      "userid": app.globalData.userId,
      "schoolid": app.globalData.schoolId,
      "contentid": contentid,
      "contenttype": contenttype,
      "content": this.data.commentValue,
      "receiverID": this.data.receiverID,
      "receiverType": this.data.receiverType,
      "receiverKidID": this.data.receiverKidID,
      "appType": app.globalData.appType,
      "appKey": app.globalData.appKey,
      "timeSpan": app.globalData.timespan,
      "mobileSerialnumber": ""
    }
    wx.request({
      url: app.globalData.serverUrl,
      data: {
        "data": JSON.stringify(data),
        'act': "send_comment"
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
      },
      success: function (res) {
        console.log(res);
        if (res.data.status == 0) {
          wx.showToast({
            title: "评论成功",
            icon: 'none',
            duration: 1000,
            mask: true
          })
          that.setData({
            show: false
          });
          that.data.page = 1
          that.getInfo('正在刷新数据')
        } else {
          wx.showToast({
            title: "评论失败",
            icon: 'none',
            duration: 1000,
            mask: true
          })
        }
      },
      fail: function (res) {
        wx.showToast({
          title: "评论失败",
          icon: 'none',
          duration: 1000,
          mask: true
        })
      },
      complete: function (res) {
        console.log('submit complete');
      }
    })
  },

  onClose1(e) {

    if (!this.data.show) {
      var index = e.currentTarget.dataset.index;
      console.log(index)
      var a = index.split(",")
      var id = a[1]
      var receiverID = this.data.list[a[0]].commentList[id].UserID
      var receiverType = this.data.list[a[0]].commentList[id].UserType
      var receiverKidID = this.data.list[a[0]].commentList[id].KidID
      this.setData({
        selectIndex: a[0],
        receiverID: receiverID,
        receiverType: receiverType,
        receiverKidID: receiverKidID
      })
    }
    if (app.globalData.userId != this.data.receiverID && this.data.ReceiverType != 1) {
      this.setData({
        show: true
      });
    }


  },
  // 点击下拉显示框
  selectTap1(e) {

    if (!this.data.show) {
      var index = e.currentTarget.dataset.index;
      console.log(index)
      var a = index.split(",")
      var id = a[1]
      var receiverID = this.data.list[a[0]].commentList[id].UserID
      var receiverType = this.data.list[a[0]].commentList[id].UserType
      var receiverKidID = this.data.list[a[0]].commentList[id].KidID
      this.setData({
        selectIndex: a[0],
        receiverID: receiverID,
        receiverType: receiverType,
        receiverKidID: receiverKidID
      })
    }

    if (app.globalData.userId != this.data.receiverID && this.data.ReceiverType != 1) {
      this.setData({
        show: !this.data.show
      });
    }
  },

  zanDetailClick: function (e) {
    var index = e.currentTarget.dataset.index;
    var zanList = this.data.list[index].zanList
    var map = JSON.stringify(zanList);
    wx.navigateTo({
      url: '../zanDetail/zanDetail?map=' + map
    })
  },


})